package com.sojpt.boot;

import java.util.Map;
import java.util.Map.Entry;

import org.springframework.aop.support.AopUtils;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.jfinal.render.JsonRender;

import net.sf.cglib.proxy.Proxy;

public abstract class SoJptConfig extends JFinalConfig{

	@SuppressWarnings("unchecked")
	@Override
	public void configRoute(Routes me) {
		Map<String, ? extends Controller> map = SpringUtil.getController();
		
		for (Entry<String, ? extends Controller> entry : map.entrySet()) { 
		  Object obj = entry.getValue();
		  Class<? extends Controller> objClz  = (Class<? extends Controller>) obj.getClass();
		  //解决在Spring上下文中无法查找自定义注解
		  if (AopUtils.isAopProxy(obj)) {
			  
			  objClz = (Class<? extends Controller>) AopUtils.getTargetClass(obj);	
			  
			  if (ClassUtils.isCglibProxyClass(objClz)) {				  
				  objClz = (Class<? extends Controller>) ClassUtils.getUserClass(objClz);
			  }
		  }
		  else if (Proxy.isProxyClass(objClz)) {
			  //
		  } else if (java.lang.reflect.Proxy.isProxyClass(objClz)) {
			  //
		  }
		  me.add(getClassRequstMapping(objClz), objClz);
		}
		
		JsonRender.addExcludedAttrs("org.springframework.web.context.request.async.WebAsyncManager.WEB_ASYNC_MANAGER",
				"org.springframework.web.servlet.HandlerMapping.bestMatchingHandler",
				"hiddenHttpMethodFilter.FILTERED",
				"org.springframework.web.servlet.DispatcherServlet.CONTEXT",
				"org.springframework.web.servlet.resource.ResourceUrlProvider",
				"characterEncodingFilter.FILTERED",
				"org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE",
				"org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER",
				"formContentFilter.FILTERED",
				"org.springframework.web.servlet.HandlerMapping.bestMatchingPattern",
				"requestContextFilter.FILTERED",
				"org.springframework.web.servlet.DispatcherServlet.OUTPUT_FLASH_MAP",
				"org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping",
				"org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER",
				"org.springframework.web.servlet.HandlerMapping.uriTemplateVariables",
				"org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER",
				"org.springframework.core.convert.ConversionService",
				"javamelody.request"
				);

	}
	
	private String getClassRequstMapping (Class<? extends Controller> objClz) {
		
		RequestMapping requestMapping = objClz.getDeclaredAnnotation(RequestMapping.class);
		if (requestMapping != null) 
			return requestMapping.value()[0];
		
		GetMapping getRequestMothed = objClz.getDeclaredAnnotation(GetMapping.class);
		if (getRequestMothed != null) 
			return getRequestMothed.value()[0];
		
	    PutMapping putRequestMothed = objClz.getDeclaredAnnotation(PutMapping.class);
	    if (putRequestMothed != null) 
	    	return putRequestMothed.value()[0];
	    
	    PostMapping postRequestMothed = objClz.getDeclaredAnnotation(PostMapping.class);
	    if (postRequestMothed != null) 
	    	return postRequestMothed.value()[0];
	    
	    DeleteMapping deleteRequestMothed = objClz.getAnnotation(DeleteMapping.class);
	    if (deleteRequestMothed != null)
	    	return deleteRequestMothed.value()[0];
		
		return null;
		
	}

}
